import { StrUtil } from '@/util/msg';
import { TreeDataNode } from 'antd';

/**
 * 格式化树形结构(角色分配权限)
 * @param {any[]} data
 * @param {DICT_TYPE.Info[]} dictData
 * @returns {[]}
 */
export const formatTreeData = (data: any[], dictData: DICT_TYPE.Info[]): TreeDataNode[] => {
  let list: any[] = [];
  return data.map((c) => {
    if (c.target === '0' && c.children && c.children.length > 0) {
      c.children = formatTreeData(c.children, dictData);
    } else if (c.target === '1') {
      list = [];
      for (let v of dictData) {
        if (c.actionType.split(StrUtil.COMMIT).some((k: string) => k === v.code)) {
          list.push({
            ...v,
            parentCode: 'actionType',
            value: `${v.id}-${c.id}-${c.code}-${v.code}`,
          });
        }
      }
      c.children = list;
      //错误 会造成 value不一致
      // c.children = dictData.filter(v => c.actionType.includes(v.code)).map((j: DICT_TYPE.DictInfo) => {
      //   console.log(c)
      //   j.value = `${j.id}-${c.id}-${j.code}`
      //   console.log(j.value)
      //   return j
      // })
    }
    c.value = `${c.id}-${c.parentId}-${c.code}`;
    return c;
  });
};

/**
 * 角色获取key值(角色管理专属)
 * @param {any[]} data
 * @param {string} keyName
 * @returns {string[]}
 */
export const getTreeDataKeys = (data: any[], keyName = 'id') => {
  let keyList: string[] = [];
  for (let c of data) {
    if (c.children && c.children.length > 0) {
      keyList.push(...getTreeDataKeys(c.children, keyName));
    }
    if (c.parentCode !== 'actionType') {
      keyList.push(c[keyName]);
    }
  }
  return keyList;
};

/**
 * 获取选中key值,角色分配权限(角色管理专属)
 * @param {any[]} data
 * @param {ROLE_TYPE.MenuList} listMap
 * @param {string} keyName
 * @returns {string[]}
 */
export const getTreeDataCheckKeys = (
  data: any[],
  listMap: ROLE_TYPE.MenuList,
  keyName = 'value',
) => {
  let keyList: string[] = [];
  for (let c of data) {
    if (c.children && c.createTime && c.children.length > 0) {
      keyList.push(...getTreeDataCheckKeys(c.children, listMap));
    }
    if (listMap[c.code]) {
      for (let children of c.children) {
        if (listMap[c.code].includes(children.code)) keyList.push(children[keyName]);
      }
    }
  }
  return keyList;
};
